﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XCT.Class;
using XCT.Functions;

namespace XCT.Forms
{
    public partial class FormLapKH_CLLH : DevExpress.XtraEditors.XtraForm
    {
        public FormLapKH_CLLH()
        {
            InitializeComponent();
        }

        private FormMain formMain;
        public FormLapKH_CLLH(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        #region "Biến"

            clsReports clsRp = new clsReports();

            string TuanKH;
            string pSolsx="";
            string pChungloai = "";
            string pDoly = "";
            string pHoavan = "";
            int ploai, flag = 0;
            string strduongdan = clsDungChung.strduongdan;
            string strbaocao = "Rpt_PhieuTK_CLLH.rpt";

        #endregion

        #region "Hàm"
        private void fChon()
        {
            if (optLSX.Checked == true)
            {
                ploai = 1;
            }
            else if (optChungLoai.Checked == true)
            {
                ploai = 2;

            }
            else if (OptHoaVan.Checked == true)
            {
                ploai = 3;

            }
            else if (OptDoly.Checked == true)
            {
                ploai = 4;

            }
            else if (OptMauMousse.Checked == true)
            {
                ploai = 5;

            }
            else 
            {
                ploai = 6;

            }
        }
        // đưa dữ liệu vào combo tuần tháng năm
        private void fKhoiTaoComBo()
        {
            for (int i = 2010; i <= DateTime.Now.Year + 1; i++)
                cmbNamKH.Properties.Items.Add(i.ToString());
                cmbNamKH.Text = DateTime.Now.Year.ToString();
                cmbThangKH.SelectedIndex = DateTime.Now.Month - 1;

        }
        // đưa dữ liệu vào lstbox lsx
        private void fDocLSXTheoTuanKH_CLLH(string TuanKH)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocLSXTheoTuanKH_CLLH(TuanKH);
            chListLSX.DataSource = ds.Tables[0].DefaultView;
            chListLSX.DisplayMember = "SoLSX";
            chListLSX.ValueMember = "SoLSX";
        }
        //đua dư liệu chung loai mousse 
        private void fDocChungLoaiMousse_CLLH(string SoLSX)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocChungLoaiMousse_CLLH(SoLSX);
            chListCL.DataSource = ds.Tables[0].DefaultView;
            chListCL.DisplayMember = "MaChungLoai_Mousse";
            chListCL.ValueMember = "MaChungLoai_Mousse";
        }
        // đưa dữ liêu vào listbox độ ly
        private void fDocDoLy_CLLH(string Solsx,string Chungloai)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocDoLy_CLLH(Solsx,Chungloai);
            chListDOLY.DataSource = ds.Tables[0].DefaultView;
            chListDOLY.DisplayMember = "doly";
            chListDOLY.ValueMember = "doly";
        }
        // đưa dữ liệu vào listbox hoa van
        private void fDocHoaVan_CLLH(string Solsx, string Chungloai, string DoLy)
        {
            DataSet ds = new DataSet();
            ds = clsRp.DocHoaVan_CLLH(Solsx, Chungloai, DoLy);
            chListHV.DataSource = ds.Tables[0].DefaultView;
            chListHV.DisplayMember = "TenHoaVan";
            chListHV.ValueMember = "MaHoaVanMousse";
        }

        private bool fKiemTraDuLieu(string strduongdan, string strbaocao)
        {
            FileInfo fileInfo = new FileInfo(strduongdan + strbaocao);
            if (fileInfo.Exists)
            {
                DataSet ds = new DataSet();
                clsRp.TuanKH = TuanKH;
                clsRp.SoLSX = pSolsx;
                clsRp.DoLy = pDoly;
                clsRp.Chungloai = pChungloai;
                clsRp.Hoavan = pHoavan;
                clsRp.Loai = ploai;

                ds = clsRp.GetDataSet(strbaocao);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    XtraMessageBox.Show("Không có dữ liệu hiển thị !", "Thông báo:");
                    return false;
                }
            }
            else
            {
                XtraMessageBox.Show("Đường dẫn report không tồn tại !", "Thông Báo:");
                return false;
            }
        }

        #endregion

        #region"Sự Kiện"

        private void FormLapKH_CLLH_Load(object sender, EventArgs e)
        {
            fKhoiTaoComBo();
           
        }

        private void FormLapKH_CLLH_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.P:
                            CmdIn_Click(sender, null);
                            return;
                    }
                }
                else if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }
                
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        // sự kiện chọn combobox
        private void cmbTuanKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            TuanKH = cmbTuanKH.Text + cmbThangKH.Text + cmbNamKH.Text;
            fDocLSXTheoTuanKH_CLLH(TuanKH);
            fDocChungLoaiMousse_CLLH("");
            fDocDoLy_CLLH("","");
            fDocHoaVan_CLLH("","","");
            chkLSX.Checked = false;
            chkCL.Checked = false;
            chkDoLy.Checked = false;
            chkHV.Checked = false;
        }

        private void chkLSX_CheckedChanged(object sender, EventArgs e)
        {
            chListLSX.BeginUpdate();
            try
            {
                flag = 1;
                for (int i = 0; i < chListLSX.ItemCount; i++)
                {
                    chListLSX.SetItemCheckState(i, chkLSX.CheckState);
                }
                pSolsx = "";

                for (int i = 0; i < chListLSX.ItemCount; i++)
                {
                    if (chListLSX.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pSolsx) == false)
                            pSolsx = pSolsx + "','";
                        pSolsx = pSolsx + chListLSX.GetItemValue(i);
                    }
                }
                fDocChungLoaiMousse_CLLH(pSolsx);
                fDocDoLy_CLLH("", "");
                fDocHoaVan_CLLH("", "", "");
                chkCL.Checked = false;
                chkDoLy.Checked = false;
                chkHV.Checked = false;

                flag = 0;
            }
            finally
            {
                chListLSX.EndUpdate();
            }
        }

        private void chkDoLy_CheckedChanged(object sender, EventArgs e)
        {
            chListDOLY.BeginUpdate();
            try
            {
                flag = 1;
                int i = 0;
                while (chListDOLY.GetItem(i) != null)
                {

                    chListDOLY.SetItemCheckState(i, chkDoLy.CheckState);
                    i++;

                }
                pDoly = "";
                for (int j = 0; j < chListDOLY.ItemCount; j++)
                {
                    if (chListDOLY.GetItemChecked(j) == true)
                    {
                        if (string.IsNullOrEmpty(pDoly) == false)
                            pDoly = pDoly + "','";
                        pDoly = pDoly + chListDOLY.GetItemValue(j);
                    }
                }
                fDocHoaVan_CLLH(pSolsx, pChungloai, pDoly);

                flag = 0;
            }
            finally
            {
                chListDOLY.EndUpdate();
            }
        }

        private void chListLSX_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (flag  == 1) return;
            pSolsx = "";

            for (int i = 0; i < chListLSX.ItemCount; i++)
            {
                if (chListLSX.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pSolsx) == false)
                        pSolsx = pSolsx + "','";
                    pSolsx = pSolsx + chListLSX.GetItemValue(i);
                }
            }
            fDocChungLoaiMousse_CLLH(pSolsx);
            fDocDoLy_CLLH("", "");
            fDocHoaVan_CLLH("","","");
            chkCL.Checked = false;
            chkDoLy.Checked = false;
            chkHV.Checked = false;
        }
        private void chListCL_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (flag == 1) return;
            pChungloai = "";
            for (int i = 0; i < chListCL.ItemCount; i++)
            {
                if (chListCL.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pChungloai) == false)
                        pChungloai = pChungloai + "','";
                        pChungloai = pChungloai + chListCL.GetItemValue(i);
                }
            }
            fDocDoLy_CLLH(pSolsx,pChungloai);
            fDocHoaVan_CLLH("", "", "");
        }

        private void chListDOLY_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (flag == 1) return;
            pDoly = "";
            for (int i = 0; i < chListDOLY.ItemCount; i++)
            {
                if (chListDOLY.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pDoly) == false)
                        pDoly = pDoly + "','";
                    pDoly = pDoly + chListDOLY.GetItemValue(i);
                }
            }
            fDocHoaVan_CLLH(pSolsx, pChungloai, pDoly);
        }

        private void chkCL_CheckedChanged(object sender, EventArgs e)
        {
            chListCL.BeginUpdate();
            try
            {
                flag = 1;
                for (int i = 0; i < chListCL.ItemCount; i++)
                {
                    chListCL.SetItemCheckState(i, chkCL.CheckState);
                }
                pChungloai = "";
                for (int i = 0; i < chListCL.ItemCount; i++)
                {
                    if (chListCL.GetItemChecked(i) == true)
                    {
                        if (string.IsNullOrEmpty(pChungloai) == false)
                            pChungloai = pChungloai + "','";
                        pChungloai = pChungloai + chListCL.GetItemValue(i);
                    }
                }
                fDocDoLy_CLLH(pSolsx, pChungloai);
                fDocHoaVan_CLLH("", "", "");

                flag = 0;
            }
            finally
            {
                chListCL.EndUpdate();
            }
        }
        private void chkHV_CheckedChanged(object sender, EventArgs e)
        {
            chListHV.BeginUpdate();
            try
            {
                for (int i = 0; i < chListHV.ItemCount; i++)
                {
                    chListHV.SetItemCheckState(i, chkHV.CheckState);
                }
            }
            finally
            {
                chListHV.EndUpdate();
            }
        }
        private void CmdIn_Click(object sender, EventArgs e)
        {
            // lấy SolSX từ CheckList 
            pSolsx = "";
            for (int i = 0; i < chListLSX.ItemCount; i++)
            {
                if (chListLSX.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pSolsx) == false)
                        pSolsx = pSolsx + "','";
                    pSolsx = pSolsx + chListLSX.GetItemValue(i);
                }
            }
            // lấy chung loai
            pChungloai = "";
            for (int i = 0; i < chListCL.ItemCount; i++)
            {
                if (chListCL.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pChungloai) == false)
                        pChungloai = pChungloai + "','";
                    pChungloai = pChungloai + chListCL.GetItemValue(i);
                }
            }

            // lấy DOLy từ CheckList 
            pDoly = "";
            for (int i = 0; i < chListDOLY.ItemCount; i++)
            {
                if (chListDOLY.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pDoly) == false)
                        pDoly = pDoly + "','";
                    pDoly = pDoly + chListDOLY.GetItemValue(i);
                }
            }

            // lấy Hoa văn từ CheckList 
            pHoavan = "";
            for (int i = 0; i < chListHV.ItemCount; i++)
            {
                if (chListHV.GetItemChecked(i) == true)
                {
                    if (string.IsNullOrEmpty(pHoavan) == false)
                        pHoavan = pHoavan + "','";
                    pHoavan = pHoavan + chListHV.GetItemValue(i);
                }
            }

            fChon();
            if (fKiemTraDuLieu(strduongdan, strbaocao) == true)
            {
                clsDungChung.pTuanKH = TuanKH;
                clsDungChung.pSoLSX = pSolsx;
                clsDungChung.pChungloai = pChungloai;
                clsDungChung.pDoLy = pDoly;
                clsDungChung.pHoaVan = pHoavan;
                clsDungChung.ploai = ploai;



                clsDungChung.strbaocao = strbaocao;

                formMain.pFormLapKH_CLLH = null;
                this.Close();
            }
        }

        private void CmdThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

       

       

        

      
       

        

        

     

        
        
    }
}